class Role:
    def __init__(self, name, sex, blood=100, equipment=None):
        self.name = name
        self.sex = sex
        self.blood = blood
        self.equipment = equipment
        self.room = None

    def attack(self, other):
        if self.room == other.room:
            if self != other:
                if self.blood > 0 and other.blood > 0:
                    reduce_blood = 5 if other.equipment else 10
                    other.blood -= reduce_blood
                    print(f"{self.name}攻击{other.name},减少{reduce_blood}点血量")
                else:
                    print("其中一方已死亡，不能攻击！")
                    exit()
            else:
                print("不能攻击自己！")
        else:
            print("攻击对象，不在同一个房间！")


class PlayRoom:
    def __init__(self, name):
        self.member = []
        self.name = name

    def join(self, role):
        if role.room == None:
            if len(self.member) < 2:
                self.member.append(role)
                role.room = self.name
            else:
                print("此房间已满！")
        else:
            print("此角色已经加入房间了")


role_dict = {}
room_dict = {}

while True:
    print("请选择：1.创建角色\t2.创建房间\t3.加入房间\t4.开始战斗\t5.退出:")
    enter_number = input("请输入你的选择:")
    if enter_number.isdigit():
        enter_number = int(enter_number)
        if enter_number == 1:
            # split用于分割字符，赋给2个变量
            name, sex = input("请输入角色的姓名与性别，空格隔开:").split()
            role_dict[name] = Role(name, sex)
            def_blood = input("请输入自定义血量(默认100):")
            if def_blood.isdigit():
                role_dict[name].blood = int(def_blood)
                print(f"自定义成功，用户{name}当前血量为{role_dict[name].blood}")
            else:
                print(f"自定义失败，用户{name}当前血量为{role_dict[name].blood}")
            wear_equipment = input("装备防弹衣请输入y(不装备防弹衣请按任意键):")
            if wear_equipment == "y":
                role_dict[name].equipment = 1
        elif enter_number == 2:
            name = input("请输入房间的名字:")
            room_dict[name] = PlayRoom(name)
            # print(room_dict)
        elif enter_number == 3:
            print(f"当前角色有{role_dict.keys()}，当前房间有{room_dict.keys()}")
            role_name, room_name = input("请输入要加入的角色和房间名，空格隔开").split()
            room_dict[room_name].join(role_dict[role_name])
        elif enter_number == 4:
            print(f"当前房间有{room_dict.keys()}")
            room_name = input("请输入房间的名字")
            if len(room_dict[room_name].member) == 2:
                while True:
                    room_dict[room_name].member[0].attack(room_dict[room_name].member[1])
                    room_dict[room_name].member[1].attack(room_dict[room_name].member[0])
            else:
                print(f"房间{room_name}人数不足2人，对战失败！")
                continue
        elif enter_number == 5:
            print("退出对战！")
            break
        else:
            print("请输入正确的数字！")
    else:
        print("请输入正确的数字！")
